package cn.edu.heuet.bnve.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.edu.heuet.bnve.model.NEUClass;

public class ClassDao {

    private Context context;
    private MyOpenHelper2 mop;
    private SQLiteDatabase db;

    public ClassDao(Context context) {
        this.context = context;
        mop = new MyOpenHelper2(context, "classlist.db", null, 1);
        db = mop.getWritableDatabase();
    }

    /**
     * 插入数据
     */
    public void insertClass(NEUClass bean){
        db.execSQL("insert into classlist(day,name,position,section,teacher,week) values(?,?,?,?,?,?)",new Object[]{
                bean.getDay(),bean.getName(),bean.getPosition(),bean.getSection(),bean.getTeacher(),bean.getWeek()});
    }

    /**
     * 查询全部
     */
    public List<NEUClass> getAll(){
        List<NEUClass> beanList = new ArrayList<NEUClass>();
        Cursor cursor = getCursor();
        if (cursor.moveToFirst()) {
            do{
                NEUClass bean = new NEUClass();
                bean.setId(cursor.getInt(0));
                bean.setDay(cursor.getInt(1));
                bean.setName(cursor.getString(2));
                bean.setPosition(cursor.getString(3));
                bean.setSection(cursor.getInt(4));
                bean.setTeacher(cursor.getString(5));
                bean.setWeek(cursor.getInt(6));
                beanList.add(bean);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return beanList;
    }

    /**
     * 查询当前周的全部
     */
    public List<NEUClass> getAll(int week){
        List<NEUClass> beanList = new ArrayList<NEUClass>();
        Cursor cursor = db.rawQuery("select * from classlist where week=?",
                new String[]{week+"",});
        if (cursor.moveToFirst()) {
            do{
                NEUClass bean = new NEUClass();
                bean.setId(cursor.getInt(0));
                bean.setDay(cursor.getInt(1));
                bean.setName(cursor.getString(2));
                bean.setPosition(cursor.getString(3));
                bean.setSection(cursor.getInt(4));
                bean.setTeacher(cursor.getString(5));
                bean.setWeek(cursor.getInt(6));
                beanList.add(bean);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return beanList;
    }

    /**
     * 获得某周 某天的课程，并排序
     * @param week
     * @param day
     * @return
     */
    public List<NEUClass> getAll(int week,int day){
        List<NEUClass> beanList = new ArrayList<NEUClass>();
        Cursor cursor = db.rawQuery("select * from classlist where week=? and day=? order by section",
                new String[]{week+"",day+""});
        if (cursor.moveToFirst()) {
            do{
                NEUClass bean = new NEUClass();
                bean.setId(cursor.getInt(0));
                bean.setDay(cursor.getInt(1));
                bean.setName(cursor.getString(2));
                bean.setPosition(cursor.getString(3));
                bean.setSection(cursor.getInt(4));
                bean.setTeacher(cursor.getString(5));
                bean.setWeek(cursor.getInt(6));
                beanList.add(bean);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return beanList;
    }
    /*
    更新数据库
     */
    public void updateDataById(int id,NEUClass bean){
        ContentValues values=new ContentValues();
        values.put("day",bean.getDay());
        values.put("name",bean.getName());
        values.put("position",bean.getPosition());
        values.put("section",bean.getSection());
        values.put("teacher",bean.getTeacher());
        values.put("week",bean.getDay());

        db.update("classlist",values,"_id=?",new String[]{id+""});
    }



    public Cursor getCursor() {
        // TODO Auto-generated method stub
        String[] columns = new String[] {
                "_id",
                "day",
                "name",
                "position",
                "section",
                "teacher",
                "week"
        };
        return db.query("classlist", columns, null, null, null, null,
                null);
    }
}
